<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\system\controller;

use app\common\controller\SystemBaseController;
use app\system\facade\DicDataFacade;
use app\system\facade\DicTypeFacade;
use hg\apidoc\annotation\Desc;
use hg\apidoc\annotation\Group;
use hg\apidoc\annotation\Method;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Sort;
use hg\apidoc\annotation\Author;
use think\App;

/**
 * 数据字典
 * @Group("System")
 * @Sort(2)
 */
class Dic extends SystemBaseController
{
    /**
     * 初始化
     */
    public function initialize()
    {
        $this->logName = "数据字典";
        parent::initialize();
    }

    /**
     * 字典类型树
     * @Author("贾二小")
     * @since 2021/11/23
     */
    public function tree()
    {
        //获取数据
        $data = DicTypeFacade::tree();
        //返回
        self::success($data);
    }

    /**
     * 字典数据列表
     * @Param("parentId", type="int", desc="字典类型")
     * @author 贾二小
     * @since 2021/11/20
     */
    public function list()
    {
        //获取参数
        $paramData = $this->request->get(['parentId']);
        //参数验证
        $this->validateData($paramData);
        //获取数据
        $data = DicDataFacade::tableList($paramData['parentId']);
        //返回
        self::table($data);
    }

    /**
     * 字典类型保存
     * @Method("PUT")
     * @Param("id", type="int", desc="唯一ID")
     * @Param("parentId", type="string", desc="父路径")
     * @Param("code", type="string", require=true, desc="编码")
     * @Param("name", type="string", require=true, desc="字典名称")
     * @Author("贾二小")
     * @since 2021/12/6
     */
    public function typeSaving()
    {
        //获取参数
        $paramData = $this->request->put(['id', 'parentId', 'code', 'name']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        DicTypeFacade::saving($paramData);
        //返回
        self::success();
    }

    /**
     * 字典类型保存
     * @Method("PUT")
     * @Param("id", type="int", desc="唯一ID")
     * @Param("parentId", type="string", desc="所属字典")
     * @Param("key", type="string", require=true, desc="项名称")
     * @Param("name", type="string", require=true, desc="键值")
     * @Param("yx", type="string", require=true, desc="是否有效")
     * @Author("贾二小")
     * @since 2021/12/6
     */
    public function dataSaving()
    {
        //获取参数
        $paramData = $this->request->put(['id', 'parentId', 'key', 'name', 'yx']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        DicDataFacade::saving($paramData);
        //返回
        self::success();
    }

    /**
     * 切换是否有效
     * @Method("PATCH")
     * @Param("id", type="int", desc="唯一ID")
     * @Param("yx", type="string", require=true, desc="是否有效")
     * @Author("贾二小")
     * @since 2021/12/6
     */
    public function changeSwitch()
    {//获取参数
        $paramData = $this->request->patch(['id', 'yx']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        DicDataFacade::saving($paramData);
        //返回
        self::success();
    }

    /**
     * 字典类型删除
     * @Desc("字典类型删除")
     * @Method("DELETE")
     * @Param("id", type="int",require=true, desc="唯一ID")
     * @Author("贾二小")
     * @since 2021/11/28
     */
    public function typeDelete()
    {
        //获取参数
        $paramData = $this->request->delete(['id']);
        //参数验证
        $this->validateData($paramData);
        //删除数据
        DicTypeFacade::deleteById($paramData['id']);
        //返回
        self::success();
    }

    /**
     * 字典数据删除
     * @Desc("字典数据删除")
     * @Method("DELETE")
     * @Param("id", type="int",require=true, desc="唯一ID")
     * @Author("贾二小")
     * @since 2021/11/28
     */
    public function dataDelete()
    {
        //获取参数
        $paramData = $this->request->delete(['id']);
        //参数验证
        $this->validateData($paramData);
        //删除数据
        DicDataFacade::deleteById($paramData['id']);
        //返回
        self::success();
    }

    /**
     * 字典数据删除(批量)
     * @Desc("字典数据删除")
     * @Method("DELETE")
     * @Param("id", type="array",childrenType="int", desc="id列表")
     * @Author("贾二小")
     * @since 2021/11/28
     */
    public function dataBatchDelete()
    {
        //获取参数
        $paramData = $this->request->delete(['id']);
        //参数验证
        $this->validateData($paramData);
        //删除数据
        DicDataFacade::deleteByIds($paramData['id']);
        //返回
        self::success();
    }
}